package com.ybb.vue.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybb.vue.domain.dto.EpmLineChartDto;
import com.ybb.vue.domain.vo.EpmStatisticsLineTodayDataVo;
import com.ybb.vue.entity.EpmAlarmNotes;
import com.ybb.vue.service.EpmAlarmNotesService;
import com.ybb.vue.mapper.EpmAlarmNotesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author apple
* @description 针对表【epm_alarm_notes(防疫管理_告警记录)】的数据库操作Service实现
* @createDate 2023-03-25 16:20:27
*/
@Service
public class EpmAlarmNotesServiceImpl extends ServiceImpl<EpmAlarmNotesMapper, EpmAlarmNotes>
implements EpmAlarmNotesService{

    @Autowired
    private EpmAlarmNotesMapper epmAlarmNotesMapper;

    @Override
    public List<EpmStatisticsLineTodayDataVo> getTodayLine(EpmLineChartDto epmLineChartDto) {
        List<EpmStatisticsLineTodayDataVo> epmStatisticsLineTodayDataVos = epmAlarmNotesMapper.selectLineData(epmLineChartDto);
        List<EpmStatisticsLineTodayDataVo> result = new ArrayList<>();
        int start = "".equals(epmLineChartDto.getStartTime()) ? 0 : Integer.valueOf(epmLineChartDto.getStartTime());
        int end = "".equals(epmLineChartDto.getEndTime()) ? 23 : Integer.valueOf(epmLineChartDto.getEndTime());
        for (int i = start; i <= end; i++) {
            EpmStatisticsLineTodayDataVo lineTodayDataVo = new EpmStatisticsLineTodayDataVo();
            lineTodayDataVo.setTime(String.valueOf(i));
            lineTodayDataVo.setNumber(0);
            result.add(lineTodayDataVo);
        }

        // 对返回数据进行赋值
        if (epmStatisticsLineTodayDataVos.size() > 0) {
            epmStatisticsLineTodayDataVos.forEach(s->{
                result.forEach(t->{
                    if (t.getTime().equals(String.valueOf(Integer.valueOf(s.getTime())))) {
                        t.setNumber(s.getNumber());
                    }
                });
            });
        }
        return result;
    }

    @Override
    public List<EpmStatisticsLineTodayDataVo> getHistoryLine(EpmLineChartDto epmLineChartDto) {
        List<EpmStatisticsLineTodayDataVo> epmStatisticsLineTodayDataVos = epmAlarmNotesMapper.selectLineDataHistory(epmLineChartDto);
        List<EpmStatisticsLineTodayDataVo> result = new ArrayList<>();
        int start = "".equals(epmLineChartDto.getStartTime()) ? 1 : Integer.parseInt(epmLineChartDto.getStartTime().substring(epmLineChartDto.getStartTime().indexOf("-") + 1));
        int end  = "".equals(epmLineChartDto.getEndTime()) ? 12 : Integer.parseInt(epmLineChartDto.getEndTime().substring(epmLineChartDto.getEndTime().indexOf("-") + 1));
        // 数据处理，防止越界
        int num1 = "".equals(epmLineChartDto.getStartTime()) ? 1 : Integer.parseInt(epmLineChartDto.getStartTime().substring(0,epmLineChartDto.getStartTime().indexOf("-")));
        int num2 = "".equals(epmLineChartDto.getEndTime()) ? 1 :  Integer.parseInt(epmLineChartDto.getEndTime().substring(0,epmLineChartDto.getEndTime().indexOf("-")));
        // 跨年份
        if (num2 > num1) {
            start = 1;
            end =12;
        }

        for (int i = start; i <= end; i++) {
            EpmStatisticsLineTodayDataVo lineTodayDataVo = new EpmStatisticsLineTodayDataVo();
            lineTodayDataVo.setTime(String.valueOf(i));
            lineTodayDataVo.setNumber(0);
            result.add(lineTodayDataVo);
        }
        // 对返回数据进行赋值
        if (epmStatisticsLineTodayDataVos.size() > 0) {
            epmStatisticsLineTodayDataVos.forEach(s->{
                result.forEach(t->{
                    if (String.valueOf(Integer.valueOf(s.getTime())).equals(t.getTime())) {
                        t.setNumber(s.getNumber());
                    }
                });
            });
        }
        return result;
    }
}
